<?php
function bsc_cfs_init_manage_menu() {
	if(function_exists("add_dashboard_page")) {
		add_dashboard_page("BSC Custom Fields Search", "BSC Custom Fields Search", "read" ,"bsc-custom-search","bsc_custom_search_page");
	
	}
}

function bsc_custom_search_page() {
	global $wpdb;
	?>
	<div class="wrap">
		<h2>BSC Custom Fields Search</h2>
		<?php if(isset($_REQUEST['_wpnonce']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'submit-bsc-field') && isset($_POST['btnSubmit'])) {
			
			global $wpdb;
			$stored_obj = array();
			for($i = 0;$i<4;$i++) 
			{
				$id = $_POST['id-'.$i];
				$data = serialize(remove_duplicate_item($_POST['data-'.$i]));
				$type = $_POST['field_type-'.$i];
				$label = trim($_POST['field_label-'.$i]);
				$cf_name = $_POST['custom_field_name-'.$i];
				$bsc_custom_field_obj = new bsc_custom_field($id,$label,$data,$type,$cf_name);
				array_push($stored_obj, $bsc_custom_field_obj);
			}
			$result = submit_data($stored_obj);	
			if($result && !is_array($result)) 
			{
				?>
					<div class="updated">
						<p><strong>Save successfully</strong></p>
					</div>
				<?php 
			} else {
				?>
					<div class="error">
						<p><strong>Save failed or nothing update</strong></p>
					<?php 
							if(!empty($result) && is_array($result)) 
							{
								foreach($result as $error) 
								{
									echo "<p><strong>".$error."</strong></p>";
								}
							}
						?>
					</div>
				<?php 	
			}
		}?>
		
		
		<?php $custom_field_names = load_custom_field();?>
	

		<form name="form_wrapper" action="index.php?page=bsc-custom-search" method="post">
			<?php 
				$results = load_data();
				
				for($i = 0;$i<4;$i++) :
					 
			 ?>
			<div id="field-wrapper-<?php echo $i; ?>" class="bsc-field-wrapper">
			<strong><span><?php echo "#".$results[$i]->field_name;?></span></strong><a href="javascript:void(0)" name="hide_form" class="hidelink">Show/Hide field</a>
				 <div>
				 	<!-- <form name="save_custom_field_config" action="index.php?page=bsc-custom-search" method="post"> -->
				 	<div style="width:50%; float:left">
				 	<h2>Field Info</h2>
				 		<p>
				 			<strong>ID: </strong><?php echo $results[$i]->id;?>
				 		</p>
				 		<p>
					 		<strong>Label:</strong> <input id="field_label-<?php echo $results[$i]->id; ?>" name="field_label-<?php echo $i;?>" class="one_line_input" value="<?php echo $results[$i]->field_name; ?>" />
					 	</p>
					 	<p>
					 		<strong>Custom Fields: </strong>
					 		<select id="custom_field_name-<?php echo $i; ?>" name="custom_field_name-<?php echo $i?>" class="one_line_input">
					 			<?php 
					 				foreach($custom_field_names as $custom_field_name) 
					 				{
					 					?>
					 						<option value="<?php echo $custom_field_name->meta_key; ?>" <?php echo ($results[$i]->custom_field_name == "{$custom_field_name->meta_key}") ? "selected='selected'" : ""; ?>><?php echo $custom_field_name->meta_key; ?></option>
										<?php 					 					
					 				}
					 			?>
					 		</select>
					 	</p>
					 	
					 	<p>
					 		<strong>Type:</strong>
					 		<select id="field_type-<?php echo $i; ?>" name="field_type-<?php echo $i?>" class="one_line_input">
					 			<option value="checkbox" <?php echo ($results[$i]->type == "checkbox") ? "selected='selected'" : ""; ?>>CheckBox</option>
					 			<option value="radio" <?php echo ($results[$i]->type == "radio") ? "selected='selected'" : ""; ?> >Radio</option>
					 		</select>
					 	</p>
					 	<p>
					 		
		<!--			 		<input type="submit" class="button" value="Delete" name="btnDelete" />-->
					 	</p>
				 	</div>
					<div style="width:50%; float:right">
							<h2>Data List</h2>
							<p>
					 			<strong>Data:</strong> <input name="field_data-<?php echo $i?>" style="width:40%;border: 1px solid #DDDDDD;" />
					 			<input type="button" name="add_field_data-<?php echo $i; ?>" value="Add" />
					 		</p>
					 		
					 		<ul class="ul_data_list-<?php echo $i; ?>">
					 			<?php $data_list = unserialize($results[$i]->data);
					 			if(!empty($data_list)) 
					 			{	
					 				$count_data = 0;
					 				foreach($data_list as $inv_data) {
					 					echo "<li>{$inv_data} - <a href='javascript:void(0)' name='remv-{$count_data}'>Remove this item</a></li>";
					 					echo "<input id='data-{$count_data}' type='hidden' name='data-{$i}[]' value='{$inv_data}' />";	
                                                                                $count_data++;
					 				}
					 			}
					 			?>
					 		</ul>
					 		<input type="hidden" name='id-<?php echo $i?>' value="<?php echo $results[$i]->id;?>" />
					</div>
					<?php wp_nonce_field("submit-bsc-field");?>
				 	<!-- </form> -->
				</div>
			</div>	
			<?php 
			$count++; 
			endfor; 
			?>
			<p><input type="submit" class="button-primary" value="Save All Fields" name="btnSubmit" /></p>
		</form>
	</div>
	
<?php } 
add_action("admin_menu","bsc_cfs_init_manage_menu");

